package sjg.DataStructures.stack;


import java.util.Scanner;

/**
 * 链表模拟栈实现入栈出栈
 */
public class LinkedListStackDemo {
    public static void main(String[] args) {
        LinkedListStack stack = new LinkedListStack(4);
        boolean loop = true; // 判断是否退出菜单
        Scanner scanner = new Scanner(System.in);
        String key = "";
        while (loop) {
            System.out.println("show: 显示栈");
            System.out.println("exit: 退出程序");
            System.out.println("push: 入栈");
            System.out.println("pop: 出栈");
            System.out.println("请输入你要执行的操作~");
            key = scanner.next();
            switch (key) {
                case "show":
                    stack.show();
                    break;
                case "exit":
                    loop = false;
                    break;
                case "push":
                    System.out.print("请输入一个数：");
                    int value = scanner.nextInt();
                    Node node = new Node(value);
                    stack.push(node);
                    break;
                case "pop":
                    Node pop = stack.pop();
                    System.out.println("出栈数据为：" + pop.toString());
                    break;
            }
        }
    }
}

class LinkedListStack {
    private Node head = new Node(0);
    private int maxSize;

    LinkedListStack(int maxSize) {
        this.maxSize = maxSize;
    }

    // 栈空？
    public boolean isEmpty() {
        if (head.getNext() == null) {
            return true;
        }
        return false;
    }

    // 栈满？
    public boolean isFull() {
        Node temp = head;
        int size = 0;
        while (temp.getNext() != null) {
            size++;
            temp = temp.getNext();
        }
        return size == maxSize;
    }

    // 压栈
    public void push(Node node) {
        if (isFull()) {
            System.out.println("栈满~");
            throw new RuntimeException("栈满~~");
        }
        Node next = head.getNext();
        head.setNext(node);
        node.setNext(next);
    }

    // 出栈
    public Node pop() {
        if (isEmpty()) {
            System.out.println("链表模拟的栈为空~~");
            throw new RuntimeException("栈空~");
        }
        Node res = head.getNext();
        if (head.getNext().getNext() != null) {
            head.setNext(head.getNext().getNext());
        } else {
            head.setNext(null);
        }
        res.setNext(null);
        return res;
    }

    // 遍历
    public void show() {
        if (isEmpty()) {
            System.out.println("栈空~");
            return;
        }
        Node temp = head;
        while (temp.getNext() != null) {
            System.out.println(temp.getNext().getNo());
            temp = temp.getNext();
        }
    }
}

class Node {
    private int no;
    private Node next;

    public Node(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                '}';
    }
}